package com.ruoyi.civil.controller;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.civil.constant.MessageConstant;
import com.ruoyi.civil.entity.Result;
import com.ruoyi.civil.utils.QiniuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.civil.domain.CivilEnginneeringEmergencyCheck;
import com.ruoyi.civil.service.ICivilEnginneeringEmergencyCheckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 应急检查Controller
 *
 * @author 小赵
 * @date 2022-04-28
 */
@RestController
@RequestMapping("/civil/emergencycheck")
public class CivilEnginneeringEmergencyCheckController extends BaseController
{
    @Autowired
    private ICivilEnginneeringEmergencyCheckService civilEnginneeringEmergencyCheckService;

    /**
     * 查询应急检查列表
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        startPage();
        List<CivilEnginneeringEmergencyCheck> list = civilEnginneeringEmergencyCheckService.selectCivilEnginneeringEmergencyCheckList(civilEnginneeringEmergencyCheck);
        return getDataTable(list);
    }

    /**
     * 导出应急检查列表
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:export')")
    @Log(title = "应急检查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        List<CivilEnginneeringEmergencyCheck> list = civilEnginneeringEmergencyCheckService.selectCivilEnginneeringEmergencyCheckList(civilEnginneeringEmergencyCheck);
        ExcelUtil<CivilEnginneeringEmergencyCheck> util = new ExcelUtil<CivilEnginneeringEmergencyCheck>(CivilEnginneeringEmergencyCheck.class);
        util.exportExcel(response, list, "应急检查数据");
    }

    /**
     * 获取应急检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:query')")
    @GetMapping(value = "/{emergencyCheckId}")
    public AjaxResult getInfo(@PathVariable("emergencyCheckId") Long emergencyCheckId)
    {
        return AjaxResult.success(civilEnginneeringEmergencyCheckService.selectCivilEnginneeringEmergencyCheckByEmergencyCheckId(emergencyCheckId));
    }

    /**
     *添加应急检查
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:add')")
    @Log(title = "添加应急检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        int result=civilEnginneeringEmergencyCheckService.insertCivilEnginneeringEmergencyCheck(civilEnginneeringEmergencyCheck);
        if(result==0){
            return new AjaxResult().error("当前公路不存在，请输入正确的道路信息");
        }else{
            return toAjax(civilEnginneeringEmergencyCheckService.insertCivilEnginneeringEmergencyCheck(civilEnginneeringEmergencyCheck));
        }
    }

    /**
     * 修改应急检查
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:edit')")
    @Log(title = "应急检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        return toAjax(civilEnginneeringEmergencyCheckService.updateCivilEnginneeringEmergencyCheck(civilEnginneeringEmergencyCheck));
    }

    /**
     * 删除应急检查
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:remove')")
    @Log(title = "应急检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{emergencyCheckIds}")
    public AjaxResult remove(@PathVariable Long[] emergencyCheckIds)
    {
        return toAjax(civilEnginneeringEmergencyCheckService.deleteCivilEnginneeringEmergencyCheckByEmergencyCheckIds(emergencyCheckIds));
    }


    //图片上传
    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try{
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            //图片上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
}
